

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[TOC] 一、Swagger 简介 OpenAPI 简介  OpenAPI 规范（以前称为 Swagger 规范）是 REST API的API 描述格式。OpenAPI 文件可以描述整个 API，包括：1）可用端点 ( &#x2F;users) 和每个端点上的操作 ( GET &#x2F;users, POST &#x2F;users)2）操作参数 每个操作的输入和输出3）身份验证方法4）联系">
<meta property="og:type" content="article">
<meta property="og:title" content="Swagger 笔记 _ Swagger 简介 _ Springfox 简介 _ Springfox 2.9.2 常用注解 _ Spring Boot 整合 Swagger2 案例">
<meta property="og:url" content="http://example.com/2022/07/04/Swagger%20%E7%AC%94%E8%AE%B0%20_%20Swagger%20%E7%AE%80%E4%BB%8B%20_%20Springfox%20%E7%AE%80%E4%BB%8B%20_%20Springfox%202.9.2%20%E5%B8%B8%E7%94%A8%E6%B3%A8%E8%A7%A3%20_%20Spring%20Boot%20%E6%95%B4%E5%90%88%20Swagger2%20%E6%A1%88%E4%BE%8B/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[TOC] 一、Swagger 简介 OpenAPI 简介  OpenAPI 规范（以前称为 Swagger 规范）是 REST API的API 描述格式。OpenAPI 文件可以描述整个 API，包括：1）可用端点 ( &#x2F;users) 和每个端点上的操作 ( GET &#x2F;users, POST &#x2F;users)2）操作参数 每个操作的输入和输出3）身份验证方法4）联系">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/41179a4c13b14e0da8cdcdda510c7df6.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/78cb5ce7b1ec42c28eb5cba714b1934d.gif#pic_center">
<meta property="article:published_time" content="2022-07-04T09:01:37.000Z">
<meta property="article:modified_time" content="2022-08-22T15:49:12.369Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Swagger">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/41179a4c13b14e0da8cdcdda510c7df6.png">
  
  
  
  <title>Swagger 笔记 _ Swagger 简介 _ Springfox 简介 _ Springfox 2.9.2 常用注解 _ Spring Boot 整合 Swagger2 案例 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Swagger 笔记 _ Swagger 简介 _ Springfox 简介 _ Springfox 2.9.2 常用注解 _ Spring Boot 整合 Swagger2 案例"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-04 17:01" pubdate>
          2022年7月4日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          12k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          98 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Swagger 笔记 _ Swagger 简介 _ Springfox 简介 _ Springfox 2.9.2 常用注解 _ Spring Boot 整合 Swagger2 案例</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[TOC]</p>
<h1 id="一、Swagger-简介"><a href="#一、Swagger-简介" class="headerlink" title="一、Swagger 简介"></a>一、Swagger 简介</h1><hr>
<p><a target="_blank" rel="noopener" href="https://swagger.io/docs/specification/about/"><strong>OpenAPI 简介</strong></a></p>
<blockquote>
<p>OpenAPI 规范（以前称为 Swagger 规范）是 REST API的API 描述格式。OpenAPI 文件可以描述整个 API，包括：<br>1）可用端点 ( &#x2F;users) 和每个端点上的操作 ( GET &#x2F;users, POST &#x2F;users)<br>2）操作参数 每个操作的输入和输出<br>3）身份验证方法<br>4）联系信息、许可、使用条款和其他信息<br>API 规范可以用 YAML 或 JSON 编写</p>
</blockquote>
<p><strong><a target="_blank" rel="noopener" href="https://swagger.io/docs/specification/about/">Swagger 简介</a></strong></p>
<blockquote>
<p>Swagger 是一个规范且完整的框架，用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。主要的 Swagger 工具包括：<br>1）Swagger Editor – 基于浏览器的编辑器，可以在其中编写 OpenAPI 规范<br>2）Swagger UI – 将 OpenAPI 规范呈现为交互式 API 文档<br>3）Swagger Codegen – 从 OpenAPI 规范生成服务器存根和客户端库</p>
</blockquote>
<p><a target="_blank" rel="noopener" href="http://c.biancheng.net/view/5532.html"><strong>Swagger 作用</strong></a></p>
<blockquote>
<p>支持 API 自动生成同步的在线文档：使用 Swagger 后可以直接通过代码生成文档，不再需要自己手动编写接口文档了，对程序员来说非常方便，可以节约写文档的时间去学习新技术</p>
</blockquote>
<blockquote>
<p>提供 Web 页面在线测试 API：Swagger 生成的文档支持在线测试。参数和格式都定好了，直接在界面上输入参数对应的值即<br>可在线测试接口</p>
</blockquote>
<h1 id="二、Springfox-简介"><a href="#二、Springfox-简介" class="headerlink" title="二、Springfox 简介"></a>二、Springfox 简介</h1><hr>
<p><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/40819897"><strong>Springfox 介绍文章</strong></a></p>
<p>Springfox 是一个使用Java语言开发开源的API Doc的框架， 它的前身是swagger-springmvc，可以将我们的Controller中的方法以文档的形式展现。官方定义为： Automated JSON API documentation for API’s built with Spring。</p>
<p>Springfox 目前有1、2、3三种版本，从v3版本开始则有较大变化，据<a target="_blank" rel="noopener" href="http://springfox.github.io/springfox/docs/current/">官方文档</a>介绍，变化如下：</p>
<ul>
<li><p>删除早期版本的一些依赖。特别是删除springfox-swagger2和springfox-swagger-ui依赖。</p>
</li>
<li><p>删除 <code>@EnableSwagger2</code> 注释</p>
</li>
<li><p>添加 <code>springfox-boot-starter</code> 支持springboot使用的起步依赖</p>
</li>
<li><p>Springfox 3.x 删除了对 guava 和其他第三方库的依赖（但仍然依赖于 spring 插件和开放 api 库，用于注释和模型）</p>
</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/40819897"><strong>Springfox 的作用</strong></a></p>
<blockquote>
<p>1）将前后端有效分离，并保证了API与文档的实时同步<br>2）使用springfox生成的接口文档直观可视，支持查看各个接口需要的参数和返回结果<br>3）springfox支持在线测试，可实时检查参数和返回值</p>
</blockquote>
<p>接下来主要以v2版本为主。</p>
<h1 id="三、Springfox2-9-2-常用注解"><a href="#三、Springfox2-9-2-常用注解" class="headerlink" title="三、Springfox2.9.2 常用注解"></a>三、Springfox2.9.2 常用注解</h1><hr>
<p><a target="_blank" rel="noopener" href="http://springfox.github.io/springfox/docs/current/#springfox-spring-mvc-and-spring-boot"><strong>Springfox官方配置文档</strong></a></p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1yi4y1F7KP?p=12"><strong>@Api</strong></a></p>
<p>作用：标记类，控制整个类生成接口信息的内容，通常写在MVC中的控制器类上。</p>
<p>常用参数说明：</p>
<ul>
<li>tags，类的名称，可以有多个值，定义几个别名，在UI视图中就会显示几个控制器访问菜单</li>
<li>description，描述，已过时</li>
</ul>
<p>使用案例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RestController</span><br><span class="hljs-meta">@RequestMapping(&quot;/user&quot;)</span><br><span class="hljs-meta">@Api(tags = &#123;&quot;用户控制层&quot;&#125;, description = &quot;描述被弃用了，但依然可以使用&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserController</span>&#123;<br>	...<br>&#125;<br></code></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1yi4y1F7KP?p=13"><strong>@ApiOperation</strong></a></p>
<p>作用：标记方法，将方法显示到Swagger生成的文档中</p>
<p>常用参数说明：</p>
<ul>
<li>value，方法的名称</li>
<li>notes，方法的记录</li>
<li>httpMethod, 请求方式</li>
</ul>
<p>使用案例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@ApiOperation(value = &quot;用户注册&quot;, httpMethod = &quot;POST&quot;)</span><br><span class="hljs-meta">@PostMapping(&quot;/register&quot;)</span><br><span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">register</span><span class="hljs-params">(User user)</span>&#123;<br>    <span class="hljs-keyword">return</span> userService.register(user) ?<br>            R.success(<span class="hljs-string">&quot;注册成功&quot;</span>, user)<br>                : R.fail(<span class="hljs-string">&quot;注册失败&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>());<br>&#125;<br></code></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1yi4y1F7KP?p=14"><strong>@ApiParam</strong></a></p>
<p>作用：描述参数，作用于方法中的参数，将参数的描述显示到文档中</p>
<p>常用参数说明：</p>
<ul>
<li>name，参数名</li>
<li>value，参数描述</li>
<li>require， true&#x2F;false，表示参数是否必要</li>
</ul>
<p>使用案例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@ApiOperation(value = &quot;用户注册&quot;, httpMethod = &quot;POST&quot;)</span><br><span class="hljs-meta">@PostMapping(&quot;/register&quot;)</span><br><span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">register</span><span class="hljs-params">(<span class="hljs-meta">@ApiParam(name=&quot;用户(user)&quot;, value = &quot;注册的用户信息&quot;)</span> User user)</span>&#123;<br>    <span class="hljs-keyword">return</span> userService.register(user) ?<br>            R.success(<span class="hljs-string">&quot;注册成功&quot;</span>, user)<br>                : R.fail(<span class="hljs-string">&quot;注册失败&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>());<br>&#125;<br></code></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1yi4y1F7KP?p=15"><strong>@ApiIgnore</strong></a></p>
<p>作用：忽略，当前注解描述的方法或类型，不生成API帮助文档</p>
<p>使用案例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@ApiIgnore</span><br><span class="hljs-meta">@ApiOperation(value = &quot;用户注册&quot;, httpMethod = &quot;POST&quot;)</span><br><span class="hljs-meta">@PostMapping(&quot;/register&quot;)</span><br><span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">register</span><span class="hljs-params">(<span class="hljs-meta">@ApiParam(name=&quot;用户(user)&quot;, value = &quot;注册的用户信息&quot;)</span> User user)</span>&#123;<br>    ...<br>&#125;<br></code></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1yi4y1F7KP?p=15"><strong>@ApiImplicitParam</strong></a></p>
<p>作用：作用于@Api类中的方法，用于描述方法中的单个参数</p>
<p>常用参数说明：</p>
<ul>
<li>name，参数名，需要与方法中的参数名对应</li>
<li>value，参数描述</li>
<li>require， true&#x2F;false，表示参数是否必要</li>
<li>dataType，参数类型<br>使用案例：<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@ApiOperation(value = &quot;用户注册&quot;, httpMethod = &quot;POST&quot;)</span><br><span class="hljs-meta">@ApiImplicitParam(name = &quot;user&quot;, value = &quot;注册的用户信息&quot;)</span><br><span class="hljs-meta">@PostMapping(&quot;/register&quot;)</span><br><span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">register</span><span class="hljs-params">(<span class="hljs-meta">@ApiParam(name=&quot;用户&quot;, value = &quot;注册的用户信息&quot;)</span> User user)</span>&#123;<br>    ...<br>&#125;<br></code></pre></td></tr></table></figure></li>
</ul>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1yi4y1F7KP?p=15"><strong>@ApiImplicitParams</strong></a></p>
<p>作用：作用于@Api类中的方法，用于描述方法中的多个参数</p>
<p>常用参数说明：{ @ApiImplicitParam(…) , @ApiImplicitParam(…) } </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@ApiOperation(value = &quot;修改用户信息&quot;, httpMethod = &quot;POST&quot;)</span><br><span class="hljs-meta">@ApiImplicitParams(&#123;</span><br><span class="hljs-meta">        @ApiImplicitParam(</span><br><span class="hljs-meta">                name = &quot;userId&quot;, value = &quot;用户的ID&quot;,</span><br><span class="hljs-meta">                required = true, dataType = &quot;long&quot;, paramType = &quot;path&quot;),</span><br><span class="hljs-meta">        @ApiImplicitParam(</span><br><span class="hljs-meta">                name = &quot;user&quot;, value = &quot;修改后的用户信息(id不可更改)&quot;,</span><br><span class="hljs-meta">                dataType = &quot;User&quot;, paramType = &quot;query&quot;)</span><br><span class="hljs-meta">&#125;)</span><br><span class="hljs-meta">@PostMapping(&quot;/update/&#123;userId&#125;&quot;)</span><br><span class="hljs-keyword">public</span> R&lt;Long&gt; <span class="hljs-title function_">modify</span><span class="hljs-params">(<span class="hljs-meta">@PathVariable</span> Long userId, User user)</span>&#123;<br>    ...<br>&#125;<br></code></pre></td></tr></table></figure>

<p> <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1yi4y1F7KP?p=17"><strong>@ApiModel</strong></a></p>
<p>作用：描述一个实体类型，这个实体类型如果称为任何一个生成api帮助文档方法的返回值类型时，该注解被解析。</p>
<p>常用参数说明：</p>
<ul>
<li>vlaue，实体类的名称（唯一）</li>
<li>description，实体类的描述</li>
</ul>
<p> <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1yi4y1F7KP?p=17"><strong>@ApiModelProperty</strong></a></p>
<p>作用：描述@ApiModel实体类型中的属性</p>
<p>常用参数说明：</p>
<ul>
<li>value，参数的名称（唯一）</li>
<li>name，参数的名称</li>
<li>required，true&#x2F;false，表示属性是否必须</li>
<li>example, 值的范例</li>
<li>hidden, true&#x2F;false，表示是否隐藏</li>
</ul>
<p>使用案例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.rbac.po;<br><br><span class="hljs-keyword">import</span> io.swagger.annotations.ApiModel;<br><span class="hljs-keyword">import</span> io.swagger.annotations.ApiModelProperty;<br><span class="hljs-keyword">import</span> lombok.Data;<br><br><span class="hljs-keyword">import</span> java.io.Serializable;<br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@ApiModel(&quot;用户表实体对象&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Serializable</span> &#123;<br>    <span class="hljs-meta">@ApiModelProperty(&quot;用户ID&quot;)</span><br>    <span class="hljs-keyword">private</span> Long id;<br><br>    <span class="hljs-meta">@ApiModelProperty(&quot;用户名&quot;)</span><br>    <span class="hljs-keyword">private</span> String name;<br><br>    <span class="hljs-meta">@ApiModelProperty(&quot;用户密码&quot;)</span><br>    <span class="hljs-keyword">private</span> String password;<br><br>    <span class="hljs-meta">@ApiModelProperty(&quot;盐&quot;)</span><br>    <span class="hljs-keyword">private</span> String salt;<br><br>    <span class="hljs-meta">@ApiModelProperty(&quot;外键: 角色表对象&quot;)</span><br>    <span class="hljs-keyword">private</span> List&lt;Role&gt; roleList;<br>&#125;<br></code></pre></td></tr></table></figure>
<h1 id="四、SpringBoot-整合-Swagger2"><a href="#四、SpringBoot-整合-Swagger2" class="headerlink" title="四、SpringBoot 整合 Swagger2"></a>四、SpringBoot 整合 Swagger2</h1><hr>
<p>接下来使用SpringBoot 整合 Swagger2，实现简单的API文档生成</p>
<p>案例的项目结构：</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/608cbeb62ee746e19f28ddd72fbc8d2b.png#">https://img-blog.csdnimg.cn/608cbeb62ee746e19f28ddd72fbc8d2b.png#</a> &#x3D;40%x)</p>
<p>运行结果：</p>
<p><img src="https://img-blog.csdnimg.cn/41179a4c13b14e0da8cdcdda510c7df6.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>版本选型：</p>
<ul>
<li>IDEA 2022 专业版</li>
<li>Maven3.8.4</li>
<li>JDK 8</li>
<li>SpringBoot 2.6.9</li>
<li>Springfox 2.9.2 </li>
<li>Springfox-ui 2.9.2</li>
</ul>
<h2 id="4-1-引入Maven依赖"><a href="#4-1-引入Maven依赖" class="headerlink" title="4.1 引入Maven依赖"></a>4.1 引入Maven依赖</h2><p>pom.xml</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">parent</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-parent<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.6.9<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">relativePath</span>/&gt;</span> <span class="hljs-comment">&lt;!-- lookup parent from repository --&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">parent</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>cn.uni<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>swagger-demo<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">name</span>&gt;</span>swagger-demo<span class="hljs-tag">&lt;/<span class="hljs-name">name</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">description</span>&gt;</span>swagger-demo<span class="hljs-tag">&lt;/<span class="hljs-name">description</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">java.version</span>&gt;</span>1.8<span class="hljs-tag">&lt;/<span class="hljs-name">java.version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.projectlombok<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>lombok<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">optional</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">optional</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>io.springfox<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>springfox-swagger-ui<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.9.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>io.springfox<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>springfox-swagger2<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.9.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">build</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">plugins</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">plugin</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-maven-plugin<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">configuration</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">excludes</span>&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">exclude</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.projectlombok<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>lombok<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>                        <span class="hljs-tag">&lt;/<span class="hljs-name">exclude</span>&gt;</span><br>                    <span class="hljs-tag">&lt;/<span class="hljs-name">excludes</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">plugin</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">plugins</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">build</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="4-2-项目配置"><a href="#4-2-项目配置" class="headerlink" title="4.2 项目配置"></a>4.2 项目配置</h2><p>Springfox  Swagger2 配置：<code>config/SwaggerConfig.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.config;<br><br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;<br><span class="hljs-keyword">import</span> springfox.documentation.builders.RequestHandlerSelectors;<br><span class="hljs-keyword">import</span> springfox.documentation.spi.DocumentationType;<br><span class="hljs-keyword">import</span> springfox.documentation.spring.web.plugins.Docket;<br><span class="hljs-keyword">import</span> springfox.documentation.swagger2.annotations.EnableSwagger2;<br><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-meta">@EnableSwagger2</span><br><span class="hljs-comment">// http://localhost:8080/swagger-ui/index.html</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SwaggerConfig</span> &#123;<br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> Docket <span class="hljs-title function_">createRestApi</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Docket</span>(DocumentationType.SWAGGER_2) <span class="hljs-comment">//</span><br>                .select()<br>                <span class="hljs-comment">// 设置扫描的路径</span><br>                .apis(RequestHandlerSelectors.basePackage(<span class="hljs-string">&quot;cn.uni.controller&quot;</span>))<br>                .build();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>SpringMVC配置，为解决SpringBoot高版本的mvc路径匹配规则与Springfox-Swagger-ui 冲突，需要进行以下的配置：<code>config/WebMvcConfig.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.config;<br><br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;<br><span class="hljs-keyword">import</span> org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;<br><span class="hljs-keyword">import</span> org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;<br><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">WebMvcConfig</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">WebMvcConfigurationSupport</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">addResourceHandlers</span><span class="hljs-params">(ResourceHandlerRegistry registry)</span> &#123;<br>        registry.addResourceHandler(<span class="hljs-string">&quot;/**&quot;</span>).addResourceLocations(<br>                <span class="hljs-string">&quot;classpath:/static/&quot;</span>);<br>        <span class="hljs-comment">// 配置 swagger 的拦截器，SpringBoot2.6.x 兼容 Swagger 2.9.2 必备</span><br>        registry.addResourceHandler(<span class="hljs-string">&quot;swagger-ui.html&quot;</span>, <span class="hljs-string">&quot;doc.html&quot;</span>).addResourceLocations(<br>                <span class="hljs-string">&quot;classpath:/META-INF/resources/&quot;</span>);<br>        registry.addResourceHandler(<span class="hljs-string">&quot;/webjars/**&quot;</span>).addResourceLocations(<br>                <span class="hljs-string">&quot;classpath:/META-INF/resources/webjars/&quot;</span>);<br>        <span class="hljs-built_in">super</span>.addResourceHandlers(registry);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="4-3-数据模型"><a href="#4-3-数据模型" class="headerlink" title="4.3 数据模型"></a>4.3 数据模型</h2><p>用户实体类：<code>pojo/po/User.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.pojo.po;<br><br><span class="hljs-keyword">import</span> io.swagger.annotations.ApiModel;<br><span class="hljs-keyword">import</span> io.swagger.annotations.ApiModelProperty;<br><span class="hljs-keyword">import</span> lombok.Data;<br><br><span class="hljs-meta">@ApiModel(value = &quot;用户&quot;, description = &quot;用户实体类&quot;)</span><br><span class="hljs-meta">@Data</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> &#123;<br>    <span class="hljs-meta">@ApiModelProperty(name=&quot;用户ID(id)&quot;, example = &quot;1&quot;)</span><br>    <span class="hljs-keyword">private</span> Integer id;<br>    <span class="hljs-meta">@ApiModelProperty(name = &quot;用户名称(username)&quot;, example = &quot;uni&quot;)</span><br>    <span class="hljs-keyword">private</span> String username;<br>    <span class="hljs-meta">@ApiModelProperty(name = &quot;用户密码(password)&quot;, example = &quot;123456&quot;)</span><br>    <span class="hljs-keyword">private</span> String password;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>前后端交互类：<code>pojo/po/R.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.pojo.po;<br><br><span class="hljs-keyword">import</span> io.swagger.annotations.ApiModel;<br><span class="hljs-keyword">import</span> io.swagger.annotations.ApiModelProperty;<br><span class="hljs-keyword">import</span> lombok.AllArgsConstructor;<br><span class="hljs-keyword">import</span> lombok.Data;<br><span class="hljs-keyword">import</span> lombok.NoArgsConstructor;<br><br><span class="hljs-keyword">import</span> java.io.Serializable;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * Restful Response  RestFul风格的统一数据响应类</span><br><span class="hljs-comment"> * TODO 实现 前端和后端通过JSON数据的传输</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@AllArgsConstructor</span><br><span class="hljs-meta">@NoArgsConstructor</span><br><span class="hljs-meta">@ApiModel(value = &quot;前后端交互&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">R</span>&lt;T&gt; <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Serializable</span> &#123;<br><br>    <span class="hljs-meta">@ApiModelProperty(value = &quot;响应代码&quot;)</span><br>    <span class="hljs-keyword">private</span> Integer code;<br>    <span class="hljs-meta">@ApiModelProperty(value=&quot;响应的信息&quot;)</span><br>    <span class="hljs-keyword">private</span> String msg;<br>    <span class="hljs-meta">@ApiModelProperty(value=&quot;响应的数据&quot;)</span><br>    <span class="hljs-keyword">private</span> T data;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 成功的响应</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> msg   响应信息</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> data  响应数据</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span>  R对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> &lt;T&gt;  数据的类型</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span>&lt;T&gt; R&lt;T&gt; <span class="hljs-title function_">success</span><span class="hljs-params">(String msg, T data)</span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">R</span>&lt;T&gt;(<span class="hljs-number">200</span>, msg, data);<br>    &#125;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 失败的响应</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> msg   响应信息</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> data  响应数据</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span>  R对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> &lt;T&gt;  数据的类型</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span>&lt;T&gt; R&lt;T&gt; <span class="hljs-title function_">fail</span><span class="hljs-params">(String msg, T data)</span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">R</span>&lt;T&gt;(<span class="hljs-number">400</span>, msg, data);<br>    &#125;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 自定义的响应</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> msg   响应信息</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> data  响应数据</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span>  R对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> &lt;T&gt;  数据的类型</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span>&lt;T&gt; R&lt;T&gt; <span class="hljs-title function_">custom</span><span class="hljs-params">(<span class="hljs-type">int</span> code, String msg, T data)</span>&#123;<br>        <span class="hljs-keyword">return</span>  <span class="hljs-keyword">new</span> <span class="hljs-title class_">R</span>&lt;T&gt;(code, msg, data);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="4-4-Controller层"><a href="#4-4-Controller层" class="headerlink" title="4.4 Controller层"></a>4.4 Controller层</h2><p>这里模拟注册与登录，调用接口后直接返回成功的信息和请求的数据。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.controller;<br><br><span class="hljs-keyword">import</span> cn.uni.pojo.po.User;<br><span class="hljs-keyword">import</span> cn.uni.pojo.po.R;<br><span class="hljs-keyword">import</span> io.swagger.annotations.*;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.PostMapping;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RestController;<br><br><span class="hljs-meta">@Api(tags = &quot;用户操作相关的控制层&quot;)</span><br><span class="hljs-meta">@RestController</span><br><span class="hljs-meta">@RequestMapping(&quot;/user&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserController</span> &#123;<br>    <span class="hljs-meta">@ApiOperation(value = &quot;用户注册&quot;, httpMethod = &quot;POST&quot;)</span><br>    <span class="hljs-meta">@ApiImplicitParam(name = &quot;user&quot;, value = &quot;注册的用户信息&quot;)</span><br>    <span class="hljs-meta">@PostMapping(&quot;/register&quot;)</span><br>    <span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">register</span><span class="hljs-params">(<span class="hljs-meta">@ApiParam(name=&quot;用户&quot;, value = &quot;注册的用户信息&quot;)</span> User user)</span>&#123;<br>        <span class="hljs-keyword">return</span> R.success(<span class="hljs-string">&quot;注册成功&quot;</span>, user);<br>    &#125;<br>    <span class="hljs-meta">@ApiOperation(value = &quot;用户登录&quot;, httpMethod = &quot;POST&quot;)</span><br>    <span class="hljs-meta">@PostMapping(&quot;/login&quot;)</span><br>    <span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">login</span><span class="hljs-params">(User user)</span>&#123;<br>        <span class="hljs-keyword">return</span> R.success(<span class="hljs-string">&quot;登录成功&quot;</span>, user);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="4-5-SpringBoot-启动类"><a href="#4-5-SpringBoot-启动类" class="headerlink" title="4.5 SpringBoot 启动类"></a>4.5 SpringBoot 启动类</h2><p><code>App.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni;<br><br><span class="hljs-keyword">import</span> org.springframework.boot.SpringApplication;<br><span class="hljs-keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication;<br><br><span class="hljs-meta">@SpringBootApplication</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">App</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        SpringApplication.run(App.class);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="4-6-运行并测试案例"><a href="#4-6-运行并测试案例" class="headerlink" title="4.6 运行并测试案例"></a>4.6 运行并测试案例</h2><p><img src="https://img-blog.csdnimg.cn/78cb5ce7b1ec42c28eb5cba714b1934d.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/API%E6%8E%A5%E5%8F%A3/" class="category-chain-item">API接口</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Swagger/">#Swagger</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Swagger 笔记 _ Swagger 简介 _ Springfox 简介 _ Springfox 2.9.2 常用注解 _ Spring Boot 整合 Swagger2 案例</div>
      <div>http://example.com/2022/07/04/Swagger 笔记 _ Swagger 简介 _ Springfox 简介 _ Springfox 2.9.2 常用注解 _ Spring Boot 整合 Swagger2 案例/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月4日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/07/06/Java%E5%AE%9E%E7%8E%B0%E5%B0%86JSON%E6%96%87%E4%BB%B6%E5%AF%BC%E5%87%BA%E5%88%B0Excel/" title="Java实现将JSON文件导出到Excel">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Java实现将JSON文件导出到Excel</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/06/25/Vue2%20%E7%AC%94%E8%AE%B05%20Vue2%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC%E9%97%AE%E9%A2%98%20_%20vue-router%E8%B7%AF%E7%94%B1%E6%A6%82%E5%BF%B5%E3%80%81%E5%88%86%E7%B1%BB%E4%B8%8E%E4%BD%BF%E7%94%A8%20_%20%E7%BC%96%E7%A8%8B%E5%BC%8F%E8%B7%AF%E7%94%B1%E5%AF%BC%E8%88%AA%20_%20%E8%B7%AF%E7%94%B1%E7%BB%84%E4%BB%B6%E7%9A%84%E7%BC%93%E5%AD%98%20_%205%E7%A7%8D%E8%B7%AF%E7%94%B1%E5%AF%BC%E8%88%AA%E5%AE%88%E5%8D%AB%20_%20%E5%B5%8C%E5%A5%97%E8%B7%AF%E7%94%B1%20_%20Vue2%E9%A1%B9%E7%9B%AE%E7%9A%84%E6%89%93%E5%8C%85%E4%B8%8E%E9%83%A8%E7%BD%B2/" title="Vue2 笔记5 Vue2页面跳转问题 _ vue-router路由概念、分类与使用 _ 编程式路由导航 _ 路由组件的缓存 _ 5种路由导航守卫 _ 嵌套路由 _ Vue2项目的打包与部署">
                        <span class="hidden-mobile">Vue2 笔记5 Vue2页面跳转问题 _ vue-router路由概念、分类与使用 _ 编程式路由导航 _ 路由组件的缓存 _ 5种路由导航守卫 _ 嵌套路由 _ Vue2项目的打包与部署</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
